/*
 *
 * -------------------------------------------------------------------------------------------------
 *                            COPYRIGHT(C) BOCO HEALTH 2016
 * -------------------------------------------------------------------------------------------------
 *
 * =================================================================================================
 *  *HISTORY
 *
 *      Tag                Date            Author           Description
 * =================================================================================================
 *      created            2017/1/17      caodailu        create
 * =================================================================================================
 *
 *
 */

package com.boco.base.base;

import android.net.Uri;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.common.RotationOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.zhy.autolayout.utils.AutoUtils;


public class BaseRcViewHolder extends BaseViewHolder {
    public BaseRcViewHolder(View view) {
        super(view);
        AutoUtils.auto(view);
    }

    /**
     * Will set the image of an ImageView from a url with imageOptions.
     */
    public BaseViewHolder setImageUrl(int viewId, String url, int width, int height) {
        SimpleDraweeView draweeView = getView(viewId);

        if (url != null) {
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                    .setResizeOptions(new ResizeOptions(width, height))//依据宽和高进行图片压缩
                    .setRotationOptions(RotationOptions.autoRotate())//设置为自动旋转
                    .setProgressiveRenderingEnabled(true)//设置渐进式图片显示
                    .build();

            PipelineDraweeControllerBuilder builder = Fresco.newDraweeControllerBuilder();
            builder.setOldController(draweeView.getController())
                    .setImageRequest(request)
                    .setAutoPlayAnimations(true);//设置自动播放gif图
            draweeView.setController(builder.build());
        }

        return this;
    }

}
